// $codepro.audit.disable
package edu.gatech.oad.cyberdoc.view.invoice;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.NumberFormat;
import java.util.Date;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.jgoodies.forms.factories.FormFactory;
import com.jgoodies.forms.layout.ColumnSpec;
import com.jgoodies.forms.layout.FormLayout;
import com.jgoodies.forms.layout.RowSpec;

public class ViewInvoiceDialog extends JDialog {

	private final JPanel contentPanel = new JPanel();

	private FormLayout layout;

	/**
	 * Create the dialog.
	 */
	public ViewInvoiceDialog(JSONObject invoice) throws JSONException {
		String date = new Date(invoice.getLong("date")).toString();
		String id = invoice.getInt("id") + "";
		String patient = invoice.getJSONObject("patient").getString("username");
		String doctor = invoice.getJSONObject("doctor").getString("username");

		setBounds(100, 100, 400, 400);
		getContentPane().setLayout(new BorderLayout());

		layout = new FormLayout(
				new ColumnSpec[] { FormFactory.RELATED_GAP_COLSPEC,
						FormFactory.DEFAULT_COLSPEC,
						FormFactory.RELATED_GAP_COLSPEC,
						FormFactory.DEFAULT_COLSPEC,
						FormFactory.RELATED_GAP_COLSPEC,
						FormFactory.DEFAULT_COLSPEC,
						FormFactory.RELATED_GAP_COLSPEC,
						FormFactory.DEFAULT_COLSPEC, }, new RowSpec[] {
						FormFactory.RELATED_GAP_ROWSPEC,
						FormFactory.DEFAULT_ROWSPEC,
						FormFactory.RELATED_GAP_ROWSPEC,
						FormFactory.DEFAULT_ROWSPEC,
						FormFactory.RELATED_GAP_ROWSPEC,
						FormFactory.DEFAULT_ROWSPEC,
						FormFactory.RELATED_GAP_ROWSPEC,
						FormFactory.DEFAULT_ROWSPEC,
						FormFactory.RELATED_GAP_ROWSPEC,
						FormFactory.DEFAULT_ROWSPEC,
						FormFactory.RELATED_GAP_ROWSPEC,
						FormFactory.DEFAULT_ROWSPEC,
						FormFactory.RELATED_GAP_ROWSPEC,
						FormFactory.DEFAULT_ROWSPEC, });

		contentPanel.setLayout(layout);

		JLabel lblDate = new JLabel("Date:");
		contentPanel.add(lblDate, "2, 2");

		JLabel lblDatedata = new JLabel(date);
		contentPanel.add(lblDatedata, "4, 2");

		JLabel lblInvoiceId = new JLabel("ID:");
		contentPanel.add(lblInvoiceId, "2, 4");

		JLabel lblInvoiceiddata = new JLabel(id);
		contentPanel.add(lblInvoiceiddata, "4, 4");

		JLabel lblPatient = new JLabel("Patient:");
		contentPanel.add(lblPatient, "2, 6");

		JLabel lblPatientdata = new JLabel(patient);
		contentPanel.add(lblPatientdata, "4, 6");

		JLabel lblDoctor = new JLabel("Doctor:");
		contentPanel.add(lblDoctor, "2, 8");

		JLabel lblDoctordata = new JLabel(doctor);
		contentPanel.add(lblDoctordata, "4, 8");

		JLabel lblCategory = new JLabel("Category    ");
		lblCategory.setFont(new Font("Lucida Grande", Font.BOLD, 13));
		contentPanel.add(lblCategory, "2, 14");

		JLabel lblDescription = new JLabel(
				"Description                               ");
		lblDescription.setFont(new Font("Lucida Grande", Font.BOLD, 13));
		contentPanel.add(lblDescription, "4, 14");

		JLabel lblCost = new JLabel("Cost      ");
		lblCost.setFont(new Font("Lucida Grande", Font.BOLD, 13));
		contentPanel.add(lblCost, "8, 14");

		JSONArray items = invoice.getJSONArray("items");
		NumberFormat formatter = NumberFormat.getCurrencyInstance();
		int row = 16;

		for (int i = 0; i < items.length(); i++) {
			row = 16 + 2 * i;
			String position = "," + row;

			JSONObject item = items.getJSONObject(i);
			String name = item.getString("name");
			String description = item.getString("description");

			String cost = formatter.format(item.getDouble("cost"));

			layout.appendRow(FormFactory.RELATED_GAP_ROWSPEC);
			layout.appendRow(FormFactory.DEFAULT_ROWSPEC);

			contentPanel.add(new JLabel(name), "2" + position);
			contentPanel.add(new JLabel(description), "4" + position);
			contentPanel.add(new JLabel(cost), "8" + position);
		}

		layout.appendRow(FormFactory.RELATED_GAP_ROWSPEC);
		layout.appendRow(FormFactory.DEFAULT_ROWSPEC);
		layout.appendRow(FormFactory.RELATED_GAP_ROWSPEC);
		layout.appendRow(FormFactory.DEFAULT_ROWSPEC);

		JLabel totalLabel = new JLabel("Total:");
		totalLabel.setFont(new Font("Lucida Grande", Font.BOLD, 13));
		contentPanel.add(totalLabel, "2, " + (row + 4));

		String total = formatter.format(invoice.getDouble("total"));
		contentPanel.add(new JLabel(total), "8, " + (row + 4));

		contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));

		getContentPane().add(contentPanel, BorderLayout.CENTER);

		JPanel buttonPane = new JPanel();
		buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT));
		getContentPane().add(buttonPane, BorderLayout.SOUTH);

		JButton okButton = new JButton("Close");
		okButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				setVisible(false);
			}
		});
		
		okButton.setActionCommand("Close");
		buttonPane.add(okButton);
		getRootPane().setDefaultButton(okButton);

		setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
		setVisible(true);
	}
}
